package org.example.likou150.circularList;


import java.util.ArrayList;

// https://leetcode.cn/problems/remove-nth-node-from-end-of-list/?envType=study-plan-v2&envId=top-interview-150
public class RemoveNthFromEnd {
    public static void main(String[] args) {

    }

    public ListNode removeNthFromEnd(ListNode head, int n) {
        ArrayList<ListNode> list = new ArrayList<>();
        ListNode h = new ListNode(0);
        h.next = head;
        ListNode temp = new ListNode();
        temp = h;
        while (temp != null){
            list.add(temp);
            temp = temp.next;
        }
        int size = list.size();
        ListNode preListNode = list.get(size - n - 1);

        if(size - n + 1 > size - 1){
            preListNode.next=null;
        }else {
            preListNode.next = list.get(size - n + 1);
        }
        return h.next;
    }

    private class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
 }
}
